package com.kwshare.system.controller;

import com.kwshare.common.api.RemoteUserService;
import com.kwshare.common.core.domain.Response;
import com.kwshare.common.core.domain.auth.LoginUser;
import com.kwshare.common.core.domain.system.Menu;
import com.kwshare.common.core.utils.SecurityUtils;
import com.kwshare.system.service.impl.MenuServiceImpl;
import com.kwshare.system.service.impl.UserRoleServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 系统菜单
 *
 * @author yangjing
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuServiceImpl menuServiceImpl;

    @Resource
    private RemoteUserService remoteUserService;

    @Resource
    private UserRoleServiceImpl userRoleService;

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public Response<?> getRouters(HttpServletRequest httpServletRequest){
        String username = SecurityUtils.getUsername(httpServletRequest);
        Response<LoginUser> userInfo = remoteUserService.userInfo(username);
        Long userId = userInfo.getData().getUser().getId();
        List<Long> rolesId = userRoleService.selectRolesByUserId(userId);
        List<Menu> list = menuServiceImpl.getMenuList(rolesId);
        List<Object> menuTrees = menuServiceImpl.buildMenuTree(list);
        return Response.ok(menuTrees);
    }

    /**
     * 获取角色菜单信息
     *
     * @param roleId 角色id
     * @return 结果
     */
    @GetMapping("getRoleMenuCheckedKeys/{roleId}")
    public Response<?> getRoleMenuTree(@PathVariable("roleId") Long roleId){
        List<Long> roleMenus = menuServiceImpl.selectMenuListByRoleId(roleId);
        return Response.ok(roleMenus);
    }

    /**
     * 获取菜单信息
     *
     * @return 结果
     */
    @GetMapping("getRoleMenu")
    public Response<?> getRoleMenu(){
        List<Menu> list = menuServiceImpl.getMenuList();
        List<Object> menuTrees = menuServiceImpl.buildRoleMenuTree(list);
        return Response.ok(menuTrees);
    }
}
